<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="height=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=qqZnk9ta0LpbYMtewsmW9Vf7bHoEeiFG"></script>
    <title>百度地图--根据企业地址信息定位</title>
      <style type="text/css">
        body,html{
            width:100%;
            height:100%;
            margin:0;
            font-family:"Microsoft YaHei"
        }
        #allmap{
            width:100%;
            height:100%
        }
        .abs{
            position: absolute;
            right:0;
            top:50%;
            z-index:1000;
            width:100%;
        }
        .navgation{
            font-size:16px;
            padding:14px 8px;
            display: inline;
            color:white;
            background:#2A81CB;
        }
        #r-result{
            width:100%;
        }
    </style>
</head>
<body>
    <div class="abs">
        <div style="text-align:right;height:35px;">
            <span class="navgation" onclick="navgation()">路线</span>
        </div>
        <div id="r-result"></div>
    </div>
    <div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
    // 百度地图API功能
    var startPoint;
    var endPoint;
    var map = new BMap.Map("allmap");    // 创建Map实例
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);  // 初始化地图,设置中心点坐标和地图级别
    map.setCurrentCity("北京");          // 设置地图显示的城市 此项是必须设置的
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放

    getPosition();
    //创建检索对象
    // var localsearch = new BMap.LocalSearch(map, {
    //     renderOptions:{map: map},
    //     pageCapacity:1,
    //     onSearchComplete:function(locateResult){
    //         console.log(locateResult)
    //     }
    // });
    // localsearch.search("北京市海淀区上地信息路1号金远见大楼");

    //获取用户传过来的企业位置信息
    function getPosition(){
        if(GetQueryString("address")){
            var address = decodeURIComponent(GetQueryString("address"));
            getPointByAddress(address);
        }else{
            alert("地址信息不正确,无法定位");
            window.location.href=encodeURI(encodeURI("http://192.168.50.212/BMap.html?address=北京市海淀区上地10街"));
            // history.go(-1);
        }
    }
    //根据企业地址逆向解析地理位置
    function getPointByAddress(address){
        // 创建地址解析器实例
        var myGeo = new BMap.Geocoder();
        // 将地址解析结果显示在地图上，并调整地图视野
        myGeo.getPoint(address, function(point){
            if (point) {
                endPoint = point;
                map.centerAndZoom(point, 16);
                var marker = new BMap.Marker(point);  // 创建标注
                map.addOverlay(marker);
                //添加信息窗口
                var opts = {
                  width : 200,     // 信息窗口宽度
                  height: 50,     // 信息窗口高度
                }
                var infoWindow = new BMap.InfoWindow("地址：" + address, opts);  // 创建信息窗口对象
                marker.addEventListener("click", function(){
                    map.openInfoWindow(infoWindow,point); //开启信息窗口
                });
            }
         },
        "北京市");
    }

    function navgation(){
        getCurrentLocation_BD();
        map.clearOverlays();
        var walking = new BMap.WalkingRoute(map, {renderOptions: {map: map, panel: "r-result", autoViewport: true}});
        walking.search(startPoint, endPoint);
    }

    //H5定位用户当前的位置
    function getCurrentLocation_H5(){
        // navigator.geolocation.getCurrentPosition(function (position) {
            //得到html5定位结果(GPS坐标)
            // var x = position.coords.longitude;
            // var y = position.coords.latitude;
            // console.log(x + "  " + y)
            var x = 116.32715863448607;
            var y = 39.990912172420714;
            var ggPoint = new BMap.Point(x,y);
            // //GPS坐标转换成百度坐标
            var convertor = new BMap.Convertor();
            var pointArr = [];
            pointArr.push(ggPoint);
            convertor.translate(pointArr, 1, 5, function(data){
                if(data.status === 0) {
                    var marker = new BMap.Marker(data.points[0]);
                    map.addOverlay(marker);
                    var label = new BMap.Label("我的位置",{offset:new BMap.Size(20,-10)});
                    marker.setLabel(label); //添加百度label
                    map.centerAndZoom(data.points[0], 11);
                    startPoint = data.points[0];
                }
            })
        // });
    }
    //百度定位用户当前位置
    function getCurrentLocation_BD(){
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function(r){
            if(this.getStatus() == BMAP_STATUS_SUCCESS){
                // var mk = new BMap.Marker(r.point);
                // map.addOverlay(mk);
                // map.panTo(r.point);
                startPoint = r.point;
                // alert('您的位置：'+r.point.lng+','+r.point.lat);
            }
            else {
                alert('定位当前位置失败'+this.getStatus());
            }
        },{enableHighAccuracy: true})
    }


    //根据key获取请求字符串中的value
    function GetQueryString(name)
    {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
</script>
